﻿using IcsSEMCtrl.Event;
using IcsSEMCtrl.Util.Client;
using Lifter.Device;
using Lifter.Device.Unit;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using TKT.Lifter.CIM.Lifter.Event.Lifter;
using TKT.Lifter.CIM.Lifter.Global;
using TKT.Lifter.CIM.Lifter.Gui.Monitor.Popups;

namespace TKT.Lifter.CIM.Lifter.Gui.Widget
{
    public partial class EFUFanControl : UserControl
    {
        /// <summary>
        /// 新风机需修改Subsystem表和Observable表信息；
        /// </summary>
        /// <param name="eCPEvent"></param>
        private delegate void delegateUpdate(FanSenderEvent eCPEvent);
        private static Dictionary<string, EFUFanControl> EFUFanControlDict = new Dictionary<string, EFUFanControl>();
        public EFUFanControl()
        {
            InitializeComponent();
            this.Size = new Size(270, 310);
            txt_SetRpm.RectColor = Color.Transparent;
            btn_SetData.FillColor = Color.DarkSeaGreen;
            btn_SetData.RectColor = Color.Transparent;
            this.UIFanControl.Value = 0;
        }
        private bool EFUStatus = false; //风机运行状态
        private bool IsStatusFlag; //推送类型
        private int Rpm;
        private string fanName;
        public string elementid;

        public string FanName { get => fanName; set => fanName = value; }
        public string ElementId
        {
            get
            {
                return this.elementid;
            }
            set
            {
                this.elementid = value;
                try
                {
                    if (!EFUFanControlDict.ContainsKey(this.ElementId))
                    {
                        EFUFanControlDict.Add(this.ElementId, this);
                    }
                }
                catch (NullReferenceException)
                {
                }
            }
        }

        public class FanSenderEventAdapter : ECPEventAdapter
        {
            public FanSenderEventAdapter()
            {
                base.Subscribe();
            }
            public override string GetSubscriptionSubject()
            {
                return FanSenderEvent.FormSubject("Fan");
            }
            public override void ReceiveECPEvent(ECPEvent ecpEvent)
            {
                EFUFanControl.UpdateFanState(ecpEvent);
            }
        }


        public static void UpdateFanState(ECPEvent ecpEvent)
        {
            FanSenderEvent efuControlEvent = (FanSenderEvent)ecpEvent;

            if (EFUFanControlDict.ContainsKey(efuControlEvent.OwnerID))
            {
                EFUFanControl eFUFanControl = EFUFanControlDict[efuControlEvent.OwnerID];
                eFUFanControl.DelegateUpdateFan(efuControlEvent);
            }
        }

        //更新风机信息;
        public void DelegateUpdateFan(FanSenderEvent e)
        {
            base.BeginInvoke(new delegateUpdate(UpdateFan), e);
        }
        public void UpdateFan(FanSenderEvent eCPEvent)
        {

            if (eCPEvent.IsrpmOrState)
            {
                if (eCPEvent.State == true)
                {
                    this.UIFanControl.BodyColor = Color.PaleGreen;
                    this.EFUStatus = true;
                    this.txt_SetRpm.Enabled = true;
                    this.btn_SetData.Enabled = true;
                }
                else
                {
                    this.EFUStatus = false;
                    this.UIFanControl.BodyColor = Color.Red;
                    this.txt_SetRpm.Enabled = false;
                    this.btn_SetData.Enabled = false;
                }
            }
            else
            {
                this.Rpm = (int)eCPEvent.RPM1;
                this.UIFanControl.Value = Rpm;
                this.lbl_NowRpm.Text = Rpm.ToString() + " r/s";
            }
        }


        //设置风机转速,写入PLC
        private void btn_SetData_Click(object sender, EventArgs e)
        {
            short setRPM = Convert.ToInt16(txt_SetRpm.Text);

            LifterSEM lifterSEM = (LifterSEM)Naming.GetDeviceManager().GetDevice();
            EFUFan efu = lifterSEM.EFUFans[this.ElementId];
            efu.O_EFU01SPEEDCHANGEREQDATA = setRPM;
            efu.tempSpeed = setRPM;
            Thread.Sleep(200);
            efu.O_EFU01SPEEDCHANGEREQ = true;
        }

        private void lbl_NowRpm_TextChanged(object sender, EventArgs e)
        {
            this.txt_SetRpm.Text = "";
        }
    }
}
